// include gulp
var gulp = require('gulp');
var watch = require('gulp-watch');
var	lr = require('tiny-lr');
	var server = lr();


var changed = require('gulp-changed');
var imagemin = require('gulp-imagemin');
var minifyHTML = require('gulp-minify-html');

var concat = require('gulp-concat');
var stripDebug = require('gulp-strip-debug');
var uglify = require('gulp-uglify');


var jsmin = require('gulp-jsmin');
var rename = require('gulp-rename');


var autoprefix = require('gulp-autoprefixer');
var minifyCSS = require('gulp-minify-css');


var gulp = require('gulp'),
	minifyCSS = require('gulp-minify-css');

var sourcemaps = require('gulp-sourcemaps');



// include plug-ins
var jshint = require('gulp-jshint');

// JS hint task
gulp.task('jshint', function() {
	gulp.src('./js/000.js')
		.pipe(jshint())
		.pipe(jshint.reporter('default'));
});

gulp.task('imagemin', function() {
	var imgSrc = './img/*',
		imgDst = './build/img';

	gulp.src(imgSrc)
		.pipe(changed(imgDst))
		.pipe(imagemin())
		.pipe(gulp.dest(imgDst));
});

gulp.task('htmlpage', function() {
	var htmlSrc = './module/**/*.html',
		htmlDst = './build/module';

	gulp.src(htmlSrc)
		.pipe(changed(htmlDst))
		.pipe(minifyHTML())
		.pipe(gulp.dest(htmlDst));
});

gulp.task('scripts', function() {
	var htmlSrc = './module/**/*.js',
		htmlDst = './build/module';

	gulp.src(htmlSrc)
		//.pipe(concat('script.js'))
		.pipe(stripDebug())
		.pipe(uglify())
		.pipe(gulp.dest('./build/module/'));
});





gulp.task('javascript', function() {
	var jsSrc = './module/**/*.js',
		jslDst = './build/module';

	gulp.src(jsSrc)
		//.pipe(sourcemaps.init())
		.pipe(jsmin())
		.pipe(uglify({
			outSourceMap: true
		}))

		//.pipe(sourcemaps.write('../module'))
		//.pipe(rename({suffix: '.min'}))
		.pipe(gulp.dest(jslDst))
});

/*
gulp.task('styles', function() {
	gulp.src(['./module*/

/*.css'])
		.pipe(concat('styles.css'))
		.pipe(autoprefix('last 2 versions'))
		.pipe(minifyCSS())
		.pipe(gulp.dest('./build/module/'));
});*/



gulp.task('minify-css', function() {
	var cssSrc = './module/**/*.css',
		cssDst = './build/module';


	gulp.src(cssSrc)
		.pipe(minifyCSS({keepBreaks:true}))
		.pipe(gulp.dest(cssDst))
});

gulp.task('spy', function() {

	server.listen(35729, function (err) {

		if (err) {
			return console.log(err)
		}
		gulp.watch("js/*.js" , function(event){
			console.log(event);
			gulp.run('default');
		});
	});

});

gulp.task('default', ['javascript','minify-css','htmlpage']);